using System;
using SimpleJSON;

namespace Gj
{
    public enum StarType
    {
        None,
        Three,
        Five,
        Ten,
        Hundred
    }

    public class StarHelper : NodeHelper
    {
        public StarType type = StarType.None;
        [SelectRef(UIKey.DATA_KEY)] public SelectKey starKey;
        private int star = -1;

        protected override BindDataType bindDataType => BindDataType.Int;

        public override void SetData(JSONObject json)
        {
            if (!starKey.IsEmpty())
            {
                var num = starKey.Get(json).AsInt;
                for (var index = 0; index < child.Count; index++)
                    if (index < num)
                        child[index].gameObject.SetActive(true);
                    else
                        child[index].gameObject.SetActive(false);
            }

            base.SetData(json);
        }

        protected override void BindData(int i)
        {
            if (star == i) return;
            star = i;
            var num = 0;

            switch (type)
            {
                case StarType.Ten:
                    num = (int)Math.Ceiling((double)star / 2);
                    break;
                case StarType.Hundred:
                    num = (int)Math.Ceiling((double)star / 20);
                    break;
                case StarType.Three:
                case StarType.Five:
                case StarType.None:
                default:
                    num = star;
                    break;
            }

            for (var index = 0; index < child.Count; index++)
                if (index < num)
                    child[index].SetData(true);
                else
                    child[index].SetData(false);
        }
    }
}